package com.ddxcloud.common.handler;

import cn.hutool.core.util.ObjectUtil;
import com.ddxcloud.common.constants.enums.ResultCodeEnum;
import com.ddxcloud.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<String> businessExceptionHandler(BusinessException e) {
        if (ObjectUtil.isEmpty(e.getMessage())) {
            return Result.fail(ResultCodeEnum.GLOBAL_ERROR.getMsg(), ResultCodeEnum.GLOBAL_ERROR);
        }
        return Result.fail(e.getMessage());
    }
}
